package actions;

import java.util.ArrayList;

import javax.ejb.EJB;

import beans.UserManager;
import beans.UserManagerLocal;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import entities.Item;
import entities.User;

public class DisplayUsersAdminAction extends ActionSupport {

	private ArrayList<User> resultUsersAdmin = null;

	@EJB
	private UserManagerLocal userMgr = new UserManager();

	public String execute() {

		Integer userIdObj = (Integer) ActionContext.getContext().getSession()
				.get("userId");
		if (userIdObj == null) {
			this.addActionError("UserID is null");
			return ERROR;
		}

		resultUsersAdmin = userMgr.getAllUsers();
		// remove self
		int userId = (Integer) ActionContext.getContext().getSession().get(
				"userId");
		for (int i = 0; i < resultUsersAdmin.size(); i++) {
			if (resultUsersAdmin.get(i).getUserId() == userId) {
				resultUsersAdmin.remove(i);
				break;
			}
		}
		return "success";
	}

	public ArrayList<User> getResultUsersAdmin() {
		return resultUsersAdmin;
	}

	public void setResultUsersAdmin(ArrayList<User> resultUsersAdmin) {
		this.resultUsersAdmin = resultUsersAdmin;
	}

	public UserManagerLocal getUserMgr() {
		return userMgr;
	}

	public void setUserMgr(UserManagerLocal userMgr) {
		this.userMgr = userMgr;
	}

}
